$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var warehouse = localStorage.getItem("warehouse");
    var pass = 1;
    var upData = {};
    var describe = "";
    $(".content .house").val(warehouse).trigger("change");

    //批号   获取 焦点
    $("input#goods_id").focus();

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        localStorage.removeItem("warehouse");
        window.location.href = "init.html";
    });

    //根据扫描内容 得到 盘条信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            var supplier = $("select.gys").val();
            if (!$(".content .house").val()) {
                pass = 1;
                $("#goods_id").val("");
                return $alert("请先选择仓库！");
            }
            $("#goods_id").val("");
            //加载提示符 显示
            loading_show("搜索中...");
            //获取工单
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/init/getAcidSemiProductWarehouse',
                data: {
                    warehouseCode: warehouse,
                    number: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        var data = res.result;
                        //加载提示符 隐藏
                        loading_hide();
                        //console.log(data);
                        if (data.goods_no) {
                            $alert("获取成功", 800);
                            describe = val;
                            $("input.batch").val(data.goods_no);
                            $("input.gg").val(Number(data.inv_spec));
                            $("input.gg").attr("data-code", data.inv_code);
                            $("input.gh").val(data.inv_model);
                            $("input.zl").val(data.goods_weight_use);
                            $("input.lh").val(data.batch_code);
                            $("input.jh").val(data.hook_no);
                        } else {
                            describe = val;
                            $("input.batch").val(data.number || "");
                            $("input.zl").val(data.weight);
                            $("input.lh").val(data.stove);
                            $("input.jh").val(data.volume);
                            $("input.gg").val(data.productnumbers.inventoryDiameter);
                            $("input.gg").attr("data-code", data.productnumbers.inventoryCode);
                            $("input.gh").val(data.productnumbers.inventorySteel);
                        }
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 获取编号
    $(".foot").on("click", ".del_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/init/getAcidNumber',
            data: {},
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    $alert("获取成功");
                    $("input.batch").val(res.result.number);
                    describe = res.result.number;
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                $alert(error.responseText);
            }
        });
    });

    //点击 确认
    $(".foot").on("click", ".add_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        //判断数据
        if (!$("input.batch").val()) {
            pass = 1;
            $alert("编号不可为空！");
            return;
        } else if (!$("input.gg").val()) {
            pass = 1;
            $alert("规格不可为空！");
            return;
        } else if (!$("input.gh").val()) {
            pass = 1;
            $alert("钢号不可为空！");
            return;
        } else if (!$("input.zl").val()) {
            pass = 1;
            $alert("重量不可为空！");
            return;
        }
        //获取 信息
        var batch = $(".content .batch").val();
        var gg = $(".content .gg").val();
        var ggCode = $(".content .gg").attr("data-code");
        var gh = $(".content .gh").val();
        var zl = $(".content .zl").val();
        var stove = $(".content .lh").val();
        var column = $(".content .jh").val();
        loading_show("入库中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/init/initAcidSemiProductWarehouse',
            data: {
                batch: batch,
                describe: describe ? describe : batch,
                warehouseCode: warehouse,
                specification: gg,
                steel: gh,
                weight: zl,
                stove: stove,
                column: column
            },
            dataType: "json",
            json: "callback",
            timeout: 60000,
            cache: false,
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    var data = res.result;
                    $alert(batch + "盘点成功");
                    $("input#goods_id").focus();
                    $(".content .batch").val("");
                    $(".content .zl").val("");
                    $(".content .jh").val("");
                    if (batch.indexOf("SX") > -1) {
                        $(".content .lh").val("");
                    }
                    if (data && data.print && data.goods_no.indexOf("SX") == 0) {
                        upData = res.result;
                        takeupPrint("acidTakeup", res.result);
                    }
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                if (error.status == 0) {
                    loading_hide();
                    $alert("请求超时，请重新提交！");
                } else {
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    });

    //重打
    $(".head .icon-dayin").on("click", function () {
        if (upData.goods_no) {
            $alert(upData.goods_no + "打印成功");
            takeupPrint("acidTakeup", upData);
        } else {
            $alert("暂无数据");
        }
    });

});

